package com.primb.bench.project.init.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.primb.bench.R;
import com.primb.bench.base.BaseActivity;
import com.primb.bench.common.constant.AppConstant;
import com.primb.bench.common.utils.RoleLevelConvert;
import com.primb.bench.project.approval.fragment.ApprovalFragment;
import com.primb.bench.project.home.fragment.HomeFragment;
import com.primb.bench.project.init.contract.MainContract;
import com.primb.bench.project.init.entity.LoginEntity;
import com.primb.bench.project.init.presenter.MainPresenterImpl;
import com.primb.bench.project.search.fragment.IndexFragment;
import com.primb.bench.project.setting.fragment.SettingFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Chen on 2017/4/14.
 * 功能描述：
 */

public class MainActivity extends BaseActivity implements MainContract.View {

    @BindView(R.id.layout_content)
    FrameLayout layoutContent;
    @BindView(R.id.iv_one)
    ImageView ivOne;
    @BindView(R.id.tv_one)
    TextView tvOne;
    @BindView(R.id.iv_two)
    ImageView ivTwo;
    @BindView(R.id.tv_two)
    TextView tvTwo;
    @BindView(R.id.iv_four)
    ImageView ivFour;
    @BindView(R.id.tv_four)
    TextView tvFour;
    @BindView(R.id.iv_three)
    ImageView ivThree;
    @BindView(R.id.tv_three)
    TextView tvThree;
    @BindView(R.id.one_layout)
    RelativeLayout oneLayout;
    @BindView(R.id.two_layout)
    RelativeLayout twoLayout;
    @BindView(R.id.three_layout)
    RelativeLayout threeLayout;
    @BindView(R.id.four_layout)
    RelativeLayout fourLayout;
    private MainContract.Presenter mPresenter;
    private List<Fragment> fragmentList = new ArrayList<>();
    private List<TextView> textList = new ArrayList<>();
    private List<ImageView> iconList = new ArrayList<>();
    private FragmentManager supportFragmentManager;
    private LoginEntity.DataBean info;

    @Override
    protected void onBasePreLayout() {
        super.onBasePreLayout();
//        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }

    @Override
    protected void onBaseIntent() {
        super.onBaseIntent();
        Intent intent = getIntent();
        info = intent.getParcelableExtra("info");

        AppConstant.ROLE_LEVEL_ENUM = RoleLevelConvert.roleConvert(info.getRoleLevel());
    }

    @Override
    protected int onBaseLayoutId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    protected void onBaseBindView() {

        textList.add(tvOne);
//        if (AppConstant.ROLE_LEVEL_ENUM != RoleLevelConvert.RoleLevel.MANAGER)
        textList.add(tvTwo);
        textList.add(tvThree);
        textList.add(tvFour);

        iconList.add(ivOne);
//        if (AppConstant.ROLE_LEVEL_ENUM != RoleLevelConvert.RoleLevel.MANAGER)
        iconList.add(ivTwo);
        iconList.add(ivThree);
        iconList.add(ivFour);

        if (AppConstant.ROLE_LEVEL_ENUM != RoleLevelConvert.RoleLevel.MANAGER)
            twoLayout.setVisibility(View.VISIBLE);
        else
            twoLayout.setVisibility(View.GONE);

    }

    @Override
    protected void onLoadData() {

        fragmentList.add(new HomeFragment());
//        if (AppConstant.ROLE_LEVEL_ENUM != RoleLevelConvert.RoleLevel.MANAGER)
        fragmentList.add(new ApprovalFragment());
        fragmentList.add(new IndexFragment());

        SettingFragment settingFragment = new SettingFragment();
        Bundle bundleSet = new Bundle();
        bundleSet.putParcelable("info", info);
        settingFragment.setArguments(bundleSet);
        fragmentList.add(settingFragment);

        supportFragmentManager = getSupportFragmentManager();

        mPresenter = new MainPresenterImpl(this);
        mPresenter.start();
    }

    @Override
    public List<Fragment> getFragmentList() {
        return fragmentList;
    }

    @Override
    public List<TextView> getBarTexts() {
        return textList;
    }

    @Override
    public List<ImageView> getBarIcons() {
        return iconList;
    }

    @Override
    public FragmentManager getActivityFragmentManager() {
        return supportFragmentManager;
    }

    @Override
    public int getLayoutId() {
        return R.id.layout_content;
    }

    @Override
    public void clearState(int[] normalIcons) {
        for (int i = 0; i < getBarIcons().size(); i++) {
            getBarIcons().get(i).setImageResource(normalIcons[i]);
            getBarTexts().get(i).setTextColor(ContextCompat.getColor(this, R.color.gray_666666));
        }
    }

    @Override
    public void selectState(int position, int[] selectIcons) {
        getBarIcons().get(position).setImageResource(selectIcons[position]);
        getBarTexts().get(position).setTextColor(ContextCompat.getColor(this, R.color.title_bar_bg));
    }

    private long exitTime = 0;

    @Override
    public void exitApp() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            showToast("再按一次退出程序");
            exitTime = System.currentTimeMillis();
        } else {
            /**
             * 退出程序
             */
            exit();
        }
    }

    @OnClick({R.id.one_layout, R.id.two_layout, R.id.four_layout, R.id.three_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.one_layout:
                mPresenter.showFragment(0);
                break;
            case R.id.two_layout:
                mPresenter.showFragment(1);
                break;
            case R.id.four_layout:
                mPresenter.showFragment(3);
                break;
            case R.id.three_layout:
                mPresenter.showFragment(2);
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitApp();
        }
        return false;
    }
}
